package com.yu.work.dao;

import com.yu.work.XUtilsApplication;
import com.yu.work.domain.Record;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

public class RecordDao {

    private DbManager db = x.getDb(XUtilsApplication.getDaoConfig());

    public void save(Record record) {
        try {
            db.save(record);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public Record get(Long id) {
        try {
            return db.findById(Record.class, id);
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Record getByTargetNumber(Long targetNumber) {
        try {
            return db.selector(Record.class).where("target_number", "=", targetNumber).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Record> getAll() {
        try {
            return db.findAll(Record.class);
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void delete(Long id) {
        try {
            db.deleteById(Record.class, id);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void update(Record record) {
        try {
            db.update(record, "remark", "updateTime");
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
